﻿#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    QWidget* oldCentral = this->takeCentralWidget();
    if (oldCentral) delete oldCentral; // 删除旧部件

    initLayout();
    initButtons();
    setCentralWidget(container);
    initConnect();
}

MainWindow::~MainWindow()
{
}

void MainWindow::initLayout()
{
    container = new QWidget(this); // 创建新的容器控件
    layout=new QGridLayout(container);
    layout->setSpacing(10);
    layout->setMargin(10);
}

void MainWindow::initButtons()
{
    cBox=new QCheckBox[3];
    cBox[0].setText("下划线");
    cBox[1].setText("斜体");
    cBox[2].setText("粗体");
    for(int i=0;i<3;i++)
    {
        layout->addWidget(&cBox[i],0,i,1,1);
    }

    rBtn=new QRadioButton[3];
    rBtn[0].setText("黑色");
    rBtn[1].setText("红色");
    rBtn[2].setText("暖色");
    bGroup=new QButtonGroup;
    for(int i=0;i<3;i++)
    {
        bGroup->addButton(&rBtn[i]);
        layout->addWidget(&rBtn[i],1,i,1,1);
    }

    textEdit=new QTextEdit;
    textEdit->setText("人才啊");
    layout->addWidget(textEdit,2,0,1,3);


    pBtn=new QPushButton[3];
    pBtn[0].setText("提交");
    pBtn[1].setText("退出");
    pBtn[2].setText("取消");
    for(int i=0;i<3;i++)
    {
        layout->addWidget(&pBtn[i],3,i,1,1);
    }

}

void MainWindow::initConnect()
{

    connect(&cBox[0],SIGNAL(clicked(bool)),this,SLOT(underLine(bool)));
    connect(&cBox[1],SIGNAL(clicked(bool)),this,SLOT(italic(bool)));
    connect(&cBox[2],SIGNAL(clicked(bool)),this,SLOT(bold(bool)));

    connect(&rBtn[0],SIGNAL(clicked()),this,SLOT(setTextColor()));
    connect(&rBtn[1],SIGNAL(clicked()),this,SLOT(setTextColor()));
    connect(&rBtn[2],SIGNAL(clicked()),this,SLOT(setTextColor()));

    connect(&pBtn[0],SIGNAL(clicked()),this,SLOT(close()));
    connect(&pBtn[1],SIGNAL(clicked()),this,SLOT(close()));
    connect(&pBtn[2],SIGNAL(clicked()),this,SLOT(close()));


}

void MainWindow::bold(bool bold)
{
    QFont font=textEdit->font();
    font.setBold(bold);
    textEdit->setFont(font);

}

void MainWindow::setTextColor()
{
    QPalette palette=textEdit->palette();
    if(rBtn[0].isChecked())
    {
        palette.setColor(QPalette::Text,Qt::red);
    }
    else if(rBtn[1].isChecked())
    {
        palette.setColor(QPalette::Text,Qt::blue);
    }
    else if(rBtn[2].isChecked())
    {
        palette.setColor(QPalette::Text,Qt::yellow);
    }
    textEdit->setPalette(palette);
}

void MainWindow::underLine(bool underLine)
{
    QFont font=textEdit->font();
    font.setUnderline(underLine);
    textEdit->setFont(font);

}

void MainWindow::italic(bool italic)
{
    QFont font=textEdit->font();
    font.setItalic(italic);
    textEdit->setFont(font);
}

